package jsyf.oa.service.wechat.service_account;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.facade.wechat.config.WeChatConst;
import jsyf.oa.facade.wechat.service_account.WechatQRCodeFacade;
import jsyf.oa.facade.wechat.service_company.TokenFacade;
import jsyf.oa.service.wechat.service_company.HttpUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class wechatQRCodeService implements WechatQRCodeFacade {

    @Resource
    private TokenFacade tokenFacade;

    @Override
    public JSONObject getQRCode(String jsonData) {

        JSONObject scene_strJsonObject = new JSONObject();
        scene_strJsonObject.put("scene_str", jsonData);
        JSONObject sceneJsonObject = new JSONObject();
        sceneJsonObject.put("scene", scene_strJsonObject);

        JSONObject data = new JSONObject();
        data.put("expire_seconds",600);
        data.put("action_name","QR_STR_SCENE");
        data.put("action_info",sceneJsonObject);

        jsonData = JSONObject.toJSONString(data);

        // 数据库中获取access_token
        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        // 拼装创建模板消息所属行业的url
        String url = WeChatConst.get_qrcode.replace("TOKEN",accessToken);
        // 调用接口查询模板id
        JSONObject jsonObject = HttpUtil.request(url, "POST", jsonData);

        return jsonObject;
    }
}
